#include "setting.h"
#include "ui_setting.h"

#include <QDebug>
#include <QDesktopServices>
#include <QMessageBox>
#include <QMouseEvent>
#include <QPainter>
#include <QPropertyAnimation>
#include <QSettings>

#include "settinghandler.h"
#include "windows.h"
#include "widget.h"


#define BORDER_COLOR QColor(74, 75, 76, 220)
#define BACKGROUND_COLOR QColor(74, 75, 76, 220)
#define RADIUS 20

const int Setting::this_height = 600;
const int Setting::this_width = 800;

static Setting* setting = nullptr;
Setting* Setting::get_instance()
{
    if (nullptr == setting)
        setting = new Setting();
    return setting;
}

Setting::Setting(QWidget *parent)
    : QWidget(parent),ui(new Ui::Setting)
{
    ui->setupUi(this);

    setAttribute(Qt::WA_TranslucentBackground);  // 设置窗口背景透明
    setWindowFlags(Qt::FramelessWindowHint);  // 让窗口无边框

    qDebug() << "Setting 创建了";

    style_set();
    other_set();
    about_set();

    // 连接关闭按钮的点击事件
    connect(ui->btn_close, &QPushButton::clicked, [=]()
    {
        // 只隐藏，不销毁
        animation_close();
    });
}

void Setting::animation_show()
{
    QPropertyAnimation *animation = new QPropertyAnimation(this, "geometry");
    animation->setDuration(300);
    animation->setStartValue(QRect((Widget::screen_width - this_width) / 2, Widget::screen_height / 2, this_width, 0));
    animation->setEndValue(QRect((Widget::screen_width - this_width) / 2, (Widget::screen_height - this_height) / 2, this_width, this_height));
    animation->start();
}

void Setting::animation_close()
{
    QPropertyAnimation *animation = new QPropertyAnimation(this, "geometry");
    animation->setDuration(200);
    animation->setStartValue(QRect(x(), y(), this_width, this_height));
    animation->setEndValue(QRect(x(), y() + height() / 2, this_width, 0));
    animation->start();
    // 放完动画再关闭
    connect(animation, &QPropertyAnimation::finished, this, [this]()
    {
        hide();
        deleteLater();
    });
}

void Setting::style_set()
{
    // 外观设置按钮
    connect(ui->btn_style, &QPushButton::clicked, [=]()
    {
        ui->stackedWidget->setCurrentIndex(0);
    });

    SettingHandler* setting_handler = SettingHandler::get_instance();

    ui->spinBox_widget_height->setValue(setting_handler->get_widget_height());
    ui->horizontalSlider_widget_hight->setValue(setting_handler->get_widget_height());
    ui->spinBox_lnk_size->setValue(setting_handler->get_lnk_size());
    ui->horizontalSlider_lnk_size->setValue(setting_handler->get_lnk_size());
    ui->spinBox_margin_bottom->setValue(setting_handler->get_margin_bottom());
    ui->horizontalSlider_margin_bottom->setValue(setting_handler->get_margin_bottom());
    ui->spinBox_radius->setValue(setting_handler->get_widget_radius());
    ui->horizontalSlider_radius->setValue(setting_handler->get_widget_radius());
    ui->cb_clock->setCheckState(setting_handler->get_show_clock() ? Qt::Checked : Qt::Unchecked);
    ui->sb_back_time->setValue(setting_handler->get_back_time());


    // 保存按钮
    connect(ui->btn_save, &QPushButton::clicked, [=]()
    {
        if (ui->spinBox_widget_height->value() < ui->spinBox_lnk_size->value())
        {
            QMessageBox::information(this, "", "图标大小不能大于导航栏高度");
            return;
        }
        if (ui->spinBox_widget_height->value() < ui->spinBox_radius->value() * 2)
        {
            QMessageBox::information(this, "", "导航栏圆角半径不能大于导航栏高度的一半");
            return;
        }

        setting_handler->set_widget_height(ui->spinBox_widget_height->value());
        setting_handler->set_margin_bottom(ui->spinBox_margin_bottom->value());
        setting_handler->set_widget_radius(ui->spinBox_radius->value());
        setting_handler->set_lnk_size(ui->spinBox_lnk_size->value());
        setting_handler->set_back_time(ui->sb_back_time->value());

        setting_handler->write_all();

        emit setting_saved();
    });

    // 时钟ckeck_box
    connect(ui->cb_clock, &QCheckBox::stateChanged, this, [setting_handler](int state)
    {
        setting_handler->set_show_clock(state);
        setting_handler->write_all();
    });

    connect(ui->horizontalSlider_widget_hight, &QSlider::valueChanged, [=]()
    {
        ui->spinBox_widget_height->setValue(ui->horizontalSlider_widget_hight->value());
    });
    connect(ui->spinBox_widget_height, &QSpinBox::valueChanged, [=]()
    {
        ui->horizontalSlider_widget_hight->setValue(ui->spinBox_widget_height->value());
    });

    connect(ui->horizontalSlider_margin_bottom, &QSlider::valueChanged, [=]()
    {
        ui->spinBox_margin_bottom->setValue(ui->horizontalSlider_margin_bottom->value());
    });
    connect(ui->spinBox_margin_bottom, &QSpinBox::valueChanged, [=]()
    {
        ui->horizontalSlider_margin_bottom->setValue(ui->spinBox_margin_bottom->value());
    });

    connect(ui->horizontalSlider_radius, &QSlider::valueChanged, [=]()
    {
        ui->spinBox_radius->setValue(ui->horizontalSlider_radius->value());
    });
    connect(ui->spinBox_radius, &QSpinBox::valueChanged, [=]()
    {
        ui->horizontalSlider_radius->setValue(ui->spinBox_radius->value());
    });

    connect(ui->horizontalSlider_lnk_size, &QSlider::valueChanged, [=]()
    {
        ui->spinBox_lnk_size->setValue(ui->horizontalSlider_lnk_size->value());
    });
    connect(ui->spinBox_lnk_size, &QSpinBox::valueChanged, [=]()
    {
        ui->horizontalSlider_lnk_size->setValue(ui->spinBox_lnk_size->value());
    });
}

void Setting::other_set()
{
    // 其他设置按钮
    connect(ui->btn_other, &QPushButton::clicked, [=]()
    {
        ui->stackedWidget->setCurrentIndex(1);
    });

    // 开机启动选框
    connect(ui->cb_start, &QCheckBox::stateChanged, this, [this](int state)
    {
        // state 2是选中 0是不选中
        switch (state)
        {
        case 0:
            set_kai_ji_start(false);
            break;
        case 2:
            set_kai_ji_start(true);
            break;
        }
    });
}

void Setting::about_set()
{
    // 关于按钮
    connect(ui->btn_about, &QPushButton::clicked, [=]()
    {
        ui->stackedWidget->setCurrentIndex(2);
    });


    // 访问官网按钮
    connect(ui->btn_visti_website, &QPushButton::clicked, [=]()
    {
        QDesktopServices::openUrl(QUrl("http://101.43.171.82:80"));
    });
}

// 设置开机自启动
void Setting::set_kai_ji_start(bool isstart)
{
     QString application_name = QApplication::applicationName();  // 获取应用名称
     QSettings *settings = new QSettings("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",
                                         QSettings::NativeFormat);
     if (isstart)
     {
         QString application_path = QApplication::applicationFilePath();  // 找到应用的目录
         settings->setValue(application_name, application_path.replace("/", "\\"));  // 写入注册表
     }
     else settings->remove(application_name);
}

void Setting::paintEvent(QPaintEvent*)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);  // 设置抗锯齿

    QPen pen(BORDER_COLOR);
    pen.setWidth(1);  // 设置边框为 1 像素
    painter.setPen(pen);

    QBrush brush(BACKGROUND_COLOR);
    painter.setBrush(brush);

    QRectF rect(0, 0, this->width(), this->height());  // 画一个和窗口一样大的矩形
    painter.drawRoundedRect(rect, RADIUS, RADIUS);  // 设置矩形的圆角
}

void Setting::mousePressEvent(QMouseEvent *ev)
{
    auto pos = ev->pos();
    // 用下面两个变量保存鼠标按下时，相对窗口左上角的点的位置
    press_x = pos.x();
    press_y = pos.y();
}

void Setting::mouseMoveEvent(QMouseEvent *ev)
{
    auto global_pos = ev->globalPosition();  // 鼠标移动时，获取相对于显示器左上角的位置
    move(global_pos.x() - press_x, global_pos.y() - press_y);
}

Setting::~Setting()
{
    qDebug() << "Setting 销毁了";
}

